﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace WellDone
{
    public sealed partial class ReOpenChecksDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bCanceled = false;
        public int iCheckNumber = 0, iSelectedIndex = 0;
        public decimal dRefundAmount;
        public string strReason = "";
        public ReOpenChecksDialog()
        {
            this.InitializeComponent();
        }

        public async Task<Boolean> init()
        {
            await loadChecks();
            return true;
        }

        private async void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
        {
            // Re-load the checks for selected date
            await loadChecks();
        }

        private async Task<Boolean> loadChecks()
        {
            // Retrieve order summary records for closed checks on selected date
            string strFilter = "TimeOpened between '" + dpSelectedDate.Date.Date.ToString() + "' and '" + dpSelectedDate.Date.Date.AddDays(1).ToString() + "'";
            Globals.OrderSummaryRecords = await Globals.client.GetOrderSummaryObjectsAsync(Globals.token, strFilter);
            string strOrderSummaryIDs = "";
            lvCurrentReOpenChecks.Items.Clear();
            if (Globals.OrderSummaryRecords.Count > 0)
            {
                foreach (ServiceReferenceOpenRPOS.OrderSummary os in Globals.OrderSummaryRecords)
                {
                    strOrderSummaryIDs += os.col_ID.ToString() + ",";
                }
                strOrderSummaryIDs = strOrderSummaryIDs.Substring(0, strOrderSummaryIDs.Length - 1);

                // Get checks associated with selected summary records
                Globals.OrderCheckRecords = await Globals.client.GetOrderCheckObjectsAsync(Globals.token, "OrderSummaryID in (" + strOrderSummaryIDs + ") and Paid=1");

                if (Globals.OrderCheckRecords.Count > 0)
                {
                    ReOpenCheck roc;
                    foreach (ServiceReferenceOpenRPOS.OrderCheck oc in Globals.OrderCheckRecords)
                    {
                        roc = new ReOpenCheck();
                        ServiceReferenceOpenRPOS.OrderSummary os = Globals.OrderSummaryRecords.Where(x => x.col_ID == oc.col_OrderSummaryID).First();
                        roc.EmployeeName=Globals.LoginRecords.Where(x => x.col_ID == os.col_LoginID).First().col_DisplayName;
                        roc.OrderCheckID = oc.col_ID.ToString();
                        roc.OrderSummaryID = oc.col_OrderSummaryID.ToString();
                        if (oc.col_PaymentType == 0) { roc.Payment = "Cash"; }
                        else if (oc.col_PaymentType == 1) { roc.Payment = "Credit";}
                        else { roc.Payment = Globals.PaymentTypeRecords.Where(x => x.col_ID == oc.col_PaymentType).First().col_Name;}
                        roc.Subtotal = oc.col_Subtotal.ToString("N");
                        roc.Tax = oc.col_Tax.ToString("N");
                        roc.Total = (oc.col_Subtotal + oc.col_Tax).ToString("N");
                        lvCurrentReOpenChecks.Items.Add(roc);
                    }
                }
            }
            return true;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            // Close the dialog...
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void lvCurrentReOpenChecks_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvCurrentReOpenChecks.SelectedItems.Count > 0)
            {
                btnReOpenCheck.IsEnabled = true;
            }
            else
            {
                btnReOpenCheck.IsEnabled = false;
            }
        }

        private async void btnReOpenCheck_Click(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("Are you sure you want to reopen the selected checks?");
            IUICommand icmdYes = new UICommand("Yes");
            IUICommand icmdNo = new UICommand("No");
            messageDialog.Commands.Add(icmdYes);
            messageDialog.Commands.Add(icmdNo);
            if (await messageDialog.ShowAsync() == icmdYes)
            {
                foreach (ReOpenCheck rpc in lvCurrentReOpenChecks.SelectedItems)
                {
                    ServiceReferenceOpenRPOS.OrderCheck oc = new ServiceReferenceOpenRPOS.OrderCheck();
                    ServiceReferenceOpenRPOS.OrderSummary os = new ServiceReferenceOpenRPOS.OrderSummary();
                    os.col_ID = Convert.ToInt32(rpc.OrderSummaryID);
                    os.col_Status = 1;
                    os.bFilterStatus = true;
                    oc.col_ID = Convert.ToInt32(rpc.OrderCheckID);
                    oc.col_Paid = false;
                    oc.bFilterPaid = true;
                    await Globals.client.UpdateOrderSummaryObjectAsync(Globals.token, os);
                    await Globals.client.UpdateOrderCheckObjectAsync(Globals.token, oc);
                }

                // Close the dialog...
                if (this.CloseRequested != null)
                {
                    this.CloseRequested(this, EventArgs.Empty);
                }
            }
        }
    }

    public class ReOpenCheck
    {
        public string EmployeeName { get; set; }
        public string OrderSummaryID { get; set; }
        public string OrderCheckID { get; set; }
        public string Payment { get; set; }
        public string Subtotal { get; set; }
        public string Tax { get; set; }
        public string Total { get; set; }
    }
}
